<!--
/**
 * ImageNavigator 组件
 * 
 * 图像导航器组件，提供图像序列的快速导航
 * 
 * @author yangzhixin
 */
-->

<template>
  <div class="image-navigator">
    <div class="navigator-header">
      <span class="navigator-title">图像导航</span>
      <q-badge :label="`${currentIndex + 1} / ${totalImages}`" color="primary" />
    </div>

    <div class="navigator-controls">
      <q-btn
        icon="first_page"
        flat
        dense
        round
        :disable="currentIndex === 0"
        @click="handleFirst"
      >
        <q-tooltip>第一张</q-tooltip>
      </q-btn>

      <q-btn
        icon="navigate_before"
        flat
        dense
        round
        :disable="currentIndex === 0"
        @click="handlePrevious"
      >
        <q-tooltip>上一张</q-tooltip>
      </q-btn>

      <q-slider
        v-model="sliderValue"
        :min="0"
        :max="totalImages - 1"
        :step="1"
        class="navigator-slider"
        @update:model-value="handleSliderChange"
      />

      <q-btn
        icon="navigate_next"
        flat
        dense
        round
        :disable="currentIndex === totalImages - 1"
        @click="handleNext"
      >
        <q-tooltip>下一张</q-tooltip>
      </q-btn>

      <q-btn
        icon="last_page"
        flat
        dense
        round
        :disable="currentIndex === totalImages - 1"
        @click="handleLast"
      >
        <q-tooltip>最后一张</q-tooltip>
      </q-btn>
    </div>

    <div v-if="showThumbnails && thumbnails.length > 0" class="navigator-thumbnails">
      <q-scroll-area style="height: 100px">
        <div class="thumbnails-container">
          <div
            v-for="(thumbnail, index) in thumbnails"
            :key="index"
            class="thumbnail-item"
            :class="{ active: index === currentIndex }"
            @click="handleThumbnailClick(index)"
          >
            <img
              v-if="thumbnail"
              :src="thumbnail"
              :alt="`图像 ${index + 1}`"
              class="thumbnail-image"
            />
            <div v-else class="thumbnail-placeholder">
              <q-icon name="image" size="24px" />
            </div>
            <span class="thumbnail-label">{{ index + 1 }}</span>
          </div>
        </div>
      </q-scroll-area>
    </div>

    <div v-if="showInfo" class="navigator-info">
      <div class="info-item">
        <span class="info-label">窗宽:</span>
        <span class="info-value">{{ windowWidth }}</span>
      </div>
      <div class="info-item">
        <span class="info-label">窗位:</span>
        <span class="info-value">{{ windowCenter }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue';

/**
 * 组件属性
 */
interface Props {
  /** 当前图像索引 */
  currentIndex?: number;
  /** 图像总数 */
  totalImages?: number;
  /** 是否显示缩略图 */
  showThumbnails?: boolean;
  /** 缩略图列表 */
  thumbnails?: string[];
  /** 是否显示信息 */
  showInfo?: boolean;
  /** 窗宽 */
  windowWidth?: number;
  /** 窗位 */
  windowCenter?: number;
}

const props = withDefaults(defineProps<Props>(), {
  currentIndex: 0,
  totalImages: 0,
  showThumbnails: false,
  thumbnails: () => [],
  showInfo: true,
  windowWidth: 0,
  windowCenter: 0,
});

/**
 * 组件事件
 */
interface Emits {
  (e: 'change', index: number): void;
  (e: 'first'): void;
  (e: 'previous'): void;
  (e: 'next'): void;
  (e: 'last'): void;
}

const emit = defineEmits<Emits>();

// 滑块值
const sliderValue = ref(props.currentIndex);

// 监听外部 currentIndex 变化
watch(
  () => props.currentIndex,
  (newValue) => {
    sliderValue.value = newValue;
  }
);

/**
 * 处理滑块变化
 */
const handleSliderChange = (value: number) => {
  emit('change', value);
};

/**
 * 处理第一张
 */
const handleFirst = () => {
  emit('first');
};

/**
 * 处理上一张
 */
const handlePrevious = () => {
  emit('previous');
};

/**
 * 处理下一张
 */
const handleNext = () => {
  emit('next');
};

/**
 * 处理最后一张
 */
const handleLast = () => {
  emit('last');
};

/**
 * 处理缩略图点击
 */
const handleThumbnailClick = (index: number) => {
  emit('change', index);
};
</script>

<style scoped lang="scss">
.image-navigator {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.navigator-header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .navigator-title {
    font-size: 14px;
    font-weight: 500;
    color: #333;
  }
}

.navigator-controls {
  display: flex;
  align-items: center;
  gap: 8px;

  .navigator-slider {
    flex: 1;
  }
}

.navigator-thumbnails {
  .thumbnails-container {
    display: flex;
    gap: 8px;
    padding: 8px 0;
  }

  .thumbnail-item {
    position: relative;
    flex-shrink: 0;
    width: 60px;
    height: 60px;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    transition: all 0.2s;

    &:hover {
      border-color: #1976d2;
      transform: scale(1.05);
    }

    &.active {
      border-color: #1976d2;
      box-shadow: 0 0 8px rgba(25, 118, 210, 0.5);
    }

    .thumbnail-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .thumbnail-placeholder {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      background-color: #e0e0e0;
      color: #999;
    }

    .thumbnail-label {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      padding: 2px 4px;
      background-color: rgba(0, 0, 0, 0.7);
      color: #fff;
      font-size: 10px;
      text-align: center;
    }
  }
}

.navigator-info {
  display: flex;
  gap: 16px;
  padding: 8px;
  background-color: #fff;
  border-radius: 4px;

  .info-item {
    display: flex;
    gap: 8px;
    font-size: 12px;

    .info-label {
      color: #666;
    }

    .info-value {
      color: #333;
      font-weight: 500;
    }
  }
}
</style>





